﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using TVT.DataAccess.Repositories;
using System.Transactions;
using TVT.Common.Exceptions;

namespace TVT.BizLayer.Entities
{
    [Serializable]
    public class Comment : BaseEntity<Comment>
    {
        #region attribute

        private static DBRepository repository = DBRepository.GetInstance();
        private int id;
        private Guid userIdFrom;        
        private Guid userIdTo;
        private string text;
        private User userFrom;
        private User userTo;

        #endregion

        #region property

            public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public Guid UserIdFrom
        {
            get { return userIdFrom; }
            set { userIdFrom = value; }
        }
        public Guid UserIdTo
        {
            get { return userIdTo; }
            set { userIdTo = value; }
        }
        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        public User UserTo
        {
            get 
            {
                if (userTo == null)
                    userTo = User.Find(this.userIdTo);
                return userTo; 
            }
            set 
            { 
                userTo = value;
                userIdTo = value.UserId;
            }
        }

        public User UserFrom
        {
            get
            {
                if (userFrom == null)
                    userFrom = User.Find(this.userIdFrom);
                return userFrom;
            }
            set
            {
                userFrom = value;
                userIdFrom = value.UserId;
            }
        }

        #endregion

        #region methods

	    public override void Insert()
	    {
		    throw new NotImplementedException();
	    }

	    public override void Update()
	    {
		    throw new NotImplementedException();
	    }

	    public override void Delete()
	    {
		    repository.DeleteComment(this.id);
	    }

	    public override void Save()
	    {
		    this.id = repository.SaveComment(this.userIdFrom, this.userIdTo, this.text);
	    }

	    public static List<Comment> FindCommentsToUser( Guid userId)
	    {
		    return MapToEntities(repository.FindCommentsToUser(userId));
	    }

        #endregion
    }
}